//
//  AccountViewController.m
//  Xing
//
//  Created by 徐番茄丶 on 16/1/21.
//  Copyright © 2016年 徐番茄丶. All rights reserved.
//

#import "AccountViewController.h"
#import "MyTools.h"

@interface AccountViewController ()
@property (weak, nonatomic) IBOutlet UIButton *mybtn;
@property (weak, nonatomic) IBOutlet UITextField *weiField;
@property (weak, nonatomic) IBOutlet UITextField *aliField;
@property (weak, nonatomic) IBOutlet UITextField *nameField;

@end

@implementation AccountViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"账户绑定";
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"arrow"] style:UIBarButtonItemStylePlain target:self action:@selector(back)];
    [self.navigationItem.leftBarButtonItem setTintColor:UIColorFromRGB(0xffffff)];
    
    self.navigationController.interactivePopGestureRecognizer.enabled = YES;
    
    self.view.backgroundColor = UIColorFromRGB(0xefeff1);
    
    _mutableDic = [[NSMutableDictionary alloc]init];

    [self viewCreate];
}



-(void)viewCreate{
    
    if ([[_upDataDic objectForKey:@"alipay_num"] length] != 0 || [[_upDataDic objectForKey:@"wx_num"]  length] != 0) {
        [_mybtn setTitle:@"修改绑定" forState:UIControlStateNormal];
        _nameField.text = [_upDataDic objectForKey:@"truename"];
    }else{
        [_mybtn setTitle:@"立即绑定" forState:UIControlStateNormal];
    }
    
    if ([[_upDataDic objectForKey:@"alipay_num"] length] != 0) {
        _aliField.text = [_upDataDic objectForKey:@"alipay_num"];
    }
    
    if ([[_upDataDic objectForKey:@"wx_num"]  length] != 0) {
        _weiField.text = [_upDataDic objectForKey:@"wx_num"];
    }
    

    UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
    btn.backgroundColor = [UIColor clearColor];
    [btn addTarget:self action:@selector(fieldBack) forControlEvents:UIControlEventTouchUpInside];
    [self.view insertSubview:btn atIndex:0];
}



- (IBAction)btnChange:(id)sender {
    
    if (_aliField.text.length == 0 && _weiField.text.length == 0){
        [self.view makeToast:@"请选择账户填写" duration:0.5 position:CSToastPositionCenter];
        
    }else if (_nameField.text.length == 0) {
        [self.view makeToast:@"请填写姓名" duration:0.5 position:CSToastPositionCenter];
        
    }else{
        [self getcard];
    }
    
}

-(void)getcard{
    if (_aliField.text.length) {
        [self.mutableDic setValue:[NSString stringWithFormat:@"%@",_aliField.text] forKey:@"alipay_num"];
    }
    if (_weiField.text.length) {
        [self.mutableDic setValue:[NSString stringWithFormat:@"%@",_weiField.text] forKey:@"wx_num"];
    }
    [self.mutableDic setValue:[NSString stringWithFormat:@"%@",_nameField.text] forKey:@"truename"];
    
    
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    NSUserDefaults *user = [[NSUserDefaults alloc]init];
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"application/json"];
    manager.requestSerializer = [AFHTTPRequestSerializer serializer];
    NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];
    [dic setValue:[NSString stringWithFormat:@"%@",[user objectForKey:@"token"]] forKey:@"user_token"];

    for (NSString * key in _mutableDic) {
        
        if ([[_mutableDic objectForKey:key] length] > 0) {
            [dic setValue:[_mutableDic objectForKey:key] forKey:key];
        }
    }
    
    [manager POST:USER_EDIT_INFO_URL parameters:dic success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:nil];
        NSString * str = [NSString stringWithFormat:@"%@",[dic objectForKey:@"status"]];
        if ([str isEqualToString:@"1"]) {
            
            [self.view makeToast:dic[@"info"] duration:0.5 position:CSToastPositionCenter];
                        
            NSLog(@"保存成功");
            [self.navigationController popViewControllerAnimated:YES];
        }else{
            [self.view makeToast:dic[@"info"] duration:0.5 position:CSToastPositionCenter];
        }
        [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
    }
          failure:^(AFHTTPRequestOperation *operation, NSError *error) {
              NSLog(@"%@",error.description);
              //       [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
              [self.view makeToast:@"网络连接失败,请检查网络连接" duration:0.5 position:CSToastPositionCenter];
          }];
}



-(void)fieldBack{
    [_weiField resignFirstResponder];
    [_aliField resignFirstResponder];
    [_nameField resignFirstResponder];
}

-(void)back{
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.translucent = YES;
    self.tabBarController.tabBar.hidden = YES;
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
